﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Actuals.CoreDomain.Business;
using Actuals.Common;
using Actuals.Business;
using Actuals.CoreDomain.Data;
using Actuals.CoreDomain;


namespace Actuals
{
    public partial class ShowReleases : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IBusinessProject businessProject = new BusinessProject();
                IList<IProject> projects = businessProject.GetProjects();
                ddlProject.DataSource = projects;
                ddlProject.DataTextField = "ProjectDescription";
                ddlProject.DataValueField = "ProjectId";
                ddlProject.DataBind(); 


            }
            else
            {

                if (Request.Form["__EVENTTARGET"] == "refresh")
                {
                    BindGridView();
                }
            }
        }

        protected string getTeam(Object teamId)
        {
            IBusinessTeam businessTeam = new BusinessTeam();
            ITeam team = businessTeam.GetTeam(int.Parse(teamId.ToString()));
            return team.TeamDescription;
        }


        protected void btnShowRelease_Click(object sender, EventArgs e)
        {
            BindGridView();
        }


        protected void BindGridView()
        {
            int projectId = int.Parse(ddlProject.SelectedValue);
            IBusinessRelease businessRelease = new BusinessRelease();
            IList<IRelease> releases = businessRelease.GetReleases(projectId);
            gvShowRelease.DataSource = releases;
            gvShowRelease.DataBind();

        }
        protected void gvShowRelease_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            string releaseId;
            switch (e.CommandName)
            {
                case "EditRelease":
                    releaseId = e.CommandArgument.ToString();
                    ClientScript.RegisterStartupScript(this.GetType(), "ShowUpdateRelease", "ShowUpdateRelease('" + releaseId + "');", true);
                    break;
            }
        }

        protected void btnDeleteRelease_Click(object sender, EventArgs e)
        {

            try
            {

                for (int i = 0; i < gvShowRelease.Rows.Count; i++)
                {
                    CheckBox cb = (CheckBox)gvShowRelease.Rows[i].FindControl("chkShowRelease");
                    Label lbl = (Label)gvShowRelease.Rows[i].FindControl("lblReleaseId");

                    if (cb.Checked)
                    {
                        string releaseId = string.Empty;
                        if (lbl != null)
                        {
                            releaseId = lbl.Text;
                            IBusinessRelease businessRelease = new BusinessRelease();
                            IRelease release = new Release();
                            release.ReleaseId=int.Parse(releaseId);
                            release.ProjectId = int.Parse(ddlProject.SelectedValue);


                            businessRelease.DeleteRelease(release);
                        }
                    }
                }

                lblMsg.Text = "Deleted";
                BindGridView();
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }

    }


}